package javbot.mail;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class MailProps extends Properties {

	private static final long serialVersionUID = 804837911172801471L;
	private static final String ENABLE_MAIL_CHECKER = "ENABLE_MAIL_CHECKER";
	private static final String MAIL_SERVER_OUT = "MAIL_SERVER_OUT";
	private static final String MAIL_SERVER_IN = "MAIL_SERVER_IN";	
	private static final String LOGIN = "LOGIN";
	private static final String PASS = "PASS";
	private static final String CHECK_INTERVAL = "CHECK_INTERVAL";
	private boolean enableMailChecker;
	private String mailServerIn;
	private String mailServerOut;
	private String login;
	private char[] pass;
	private String checkInterval;
	public final static File file = new File("MailData.txt");
	private DesEncrypter encrypter;
	
	public MailProps() {
		super();
		
		encrypter = new DesEncrypter("JAVBOT");
		initMailData();
		
		try {
			load(new FileInputStream(file));
			enableMailChecker = 
				(getProperty(ENABLE_MAIL_CHECKER) != null && 
				getProperty(ENABLE_MAIL_CHECKER).toLowerCase().trim().equals("true"));
			mailServerIn = getProperty(MAIL_SERVER_IN);
			mailServerOut = getProperty(MAIL_SERVER_OUT);
			login = getProperty(LOGIN);
			pass = encrypter.decrypt(getProperty(PASS)).toCharArray();
			checkInterval = getProperty(CHECK_INTERVAL);
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void saveMailProps() throws FileNotFoundException, IOException {
		StringBuffer passStr = new StringBuffer(pass.length);
		for (int i = 0; i < pass.length; i++) {
			passStr.append(pass[i]);
		}
		setProperty(PASS,  encrypter.encrypt(passStr.toString()));
		setProperty(LOGIN, login);		
		setProperty(MAIL_SERVER_OUT, mailServerOut);		
		setProperty(MAIL_SERVER_IN, mailServerIn);		
		setProperty(ENABLE_MAIL_CHECKER, new Boolean(enableMailChecker).toString());		
		store(new FileOutputStream(file),"");
	}


	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getMailServerIn() {
		return mailServerIn;
	}

	public void setMailServerIn(String mailServerIn) {
		this.mailServerIn = mailServerIn;
	}

	public String getMailServerOut() {
		return mailServerOut;
	}

	public void setMailServerOut(String mailServerOut) {
		this.mailServerOut = mailServerOut;
	}

	public char[] getPass() {
		return pass;
	}

	public void setPass(char[] pass) {
		this.pass = pass;
	}

	
	public int getCheckInterval() {
		int i;
		try {
			i = Integer.parseInt(checkInterval);
		} catch (NumberFormatException e) {
			i = 10;
		}
		
		
		return i;
	}	
	
	
	public void setCheckInterval(int checkInterval) {
		this.checkInterval = Integer.toString(checkInterval);		
	}	
	
	public static void initMailData() {
		if (!file.exists()) {
			System.out.println("Creating MailData.txt file...");
			try {
				FileWriter fw = new FileWriter(file);
				fw.write("ENABLE_MAIL_CHECKER=false\r\n" +
						"MAIL_SERVER_IN=\r\n" +
						"MAIL_SERVER_OUT=\r\n" +
						"LOGIN=\r\n" +
						"PASS=\r\n");
				fw.flush();
				fw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
	}

	public boolean isEnableMailChecker() {
		return enableMailChecker;
	}

	public void setEnableMailChecker(boolean enableMailChecker) {
		this.enableMailChecker = enableMailChecker;
	}


}
